export const cloneOf = (obj) => {
    return JSON.parse(JSON.stringify(obj));
};

export const trim = (string, character) => {
    const first = [ ...string ].findIndex(char => char !== character);
    const last  = [ ...string ].reverse().findIndex(char => char !== character);
    return string.substring(first, string.length - last);
};

export const basename = (str) => {
    return str.split('\\').pop().split('/').pop();
};

export const getFileExtension = (filename) => {
    var ext = /^.+\.([^.]+)$/.exec(filename);
    return ext == null ? "" : ext[ 1 ];
};


const FileType = {
    'image': 'png|jpg|jpeg|tmp|gif',
    'word' : 'doc|docx',
    'ppt'  : 'ppt|pptx',
    'pdf'  : 'pdf',
    'code' : 'php|js|java|python|ruby|go|c|cpp|sql|m|h|json|html|aspx',
    'zip'  : 'zip|tar\.gz|rar|rpm',
    'txt'  : 'txt|pac|log|md',
    'audio': 'mp3|wav|flac|3pg|aa|aac|ape|au|m4a|mpc|ogg',
    'video': 'mkv|rmvb|flv|mp4|avi|wmv|rm|asf|mpeg|mov',
};

export const previewType = (ext) => {

    for (let [ key, value ] of Object.entries(FileType)) {
        let reg = new RegExp(`^(${ value })$`);
        if (reg.exec(ext.toLowerCase()) !== null) return key;
    }

    return 'unknown'
};
